/**
 * 
 */
package com.hand.cloud.b2b.daos.impl;

import de.hybris.platform.catalog.model.CatalogModel;
import de.hybris.platform.catalog.model.CatalogVersionModel;
import de.hybris.platform.category.model.CategoryModel;
import de.hybris.platform.servicelayer.model.ModelService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hand.cloud.b2b.daos.CategoryDAO;
import com.hand.cloud.b2b.model.EPCatalogModel;
import com.hand.cloud.b2b.model.EPCatalogVersionModel;
import com.hand.cloud.b2b.model.EPCategoryModel;


/**
 * @author zezhan.xue
 * @time Aug 15, 2014 10:23:36 AM
 * 
 */
@Component(value = "categoryDAO")
public class DefaultCategoryDAO implements CategoryDAO
{

	@Autowired
	private ModelService modelService;

	/**
	 * 创建CategoryModel
	 * 
	 * @return
	 */
	@Override
	public CategoryModel createCategory(EPCategoryModel epCategory)
	{
		//初始化各个字段
		CategoryModel category = this.modelService.create(CategoryModel.class);
		CatalogModel catalog = new CatalogModel();
		EPCatalogModel epcatalog = epCategory.getEpCatalogVersion().getEpCatalog();
		catalog.setDefaultCatalog(epcatalog.getDefaultCatalog());
		catalog.setId(epcatalog.getId());

		CatalogVersionModel catalogversion = createEPCatalogVersion(epCategory, catalog);
		
		category.setCatalogVersion(catalogversion);
		category.setCode(epCategory.getCode());

		this.modelService.save(category);
		return category;
	}

	/**
	 * @param epCategory
	 * @param catalog
	 * @return
	 */
	private CatalogVersionModel createEPCatalogVersion(EPCategoryModel epCategory, CatalogModel catalog)
	{
		CatalogVersionModel catalogversion = new CatalogVersionModel();
		EPCatalogVersionModel epcatalogversion = epCategory.getEpCatalogVersion();
		catalogversion.setInclAssurance(epcatalogversion.getInclAssurance());
		catalogversion.setInclDuty(epcatalogversion.getInclDuty());
		catalogversion.setInclFreight(epcatalogversion.getInclFreight());
		catalogversion.setInclPacking(epcatalogversion.getInclPacking());
		catalogversion.setVersion(epcatalogversion.getVersion());
		catalogversion.setCatalog(catalog);
		return catalogversion;
	}

}
